package com.kun.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.kun.entity.BorrowRecord;
import com.kun.entity.BorrowRecordExample;
import com.kun.entity.BorrowRecordExample.Criteria;
import com.kun.mapper.cluster.custom.BorrowMapperRCustom;
import com.kun.mapper.master.MasterBorrowRecordMapper;
import com.kun.service.BorrowService;
import com.kun.util.PaginationUtil;

/**
 * @description:
 * @author: KM
 * 2018年4月5日
 */
@Service
public class BorrowServiceImpl implements BorrowService {

	@Autowired
	private BorrowMapperRCustom clusterBorrowRecordMapper;
	
	@Autowired
	private MasterBorrowRecordMapper masterBorrowRecordMapper;
	
	@Override
	public int insertBorrow(BorrowRecord br) {
		return masterBorrowRecordMapper.insert(br);
	}

	public int updateBorrow(BorrowRecord br){
		return masterBorrowRecordMapper.updateByPrimaryKeySelective(br);
	}
	
	/**
	 * 分页条件查询
	 * */
	@SuppressWarnings("rawtypes")
	public Map<String,Object> selectAllBorrow(int begin,int length,String jsonString,List<String> userIdList,List<Integer> bookIdList){
		Map<String,Object> resultMap = new HashMap<String,Object>();
		BorrowRecordExample bex = new BorrowRecordExample();
		Criteria criteria = bex.createCriteria();
		if(userIdList!=null){
			if(userIdList.size()<1){
				return resultMap;//不存在该用户昵称 直接返回空resultMap
			}
			criteria.andUserIdIn(userIdList);//用户昵称 真实姓名 查询
		}
		if(bookIdList!=null){
			if(bookIdList.size()<1){
				return resultMap;//不存在该图书名称 直接返回空resultMap
			}
			criteria.andBookIdIn(bookIdList);//图书名称 查询
		}
		bex.setOrderByClause("is_deal  asc,borrow_Time desc");
		if(!"".equals(jsonString)&&jsonString!=null&&jsonString.length()>0){
			JSONObject json = JSONObject.parseObject(jsonString);
			Map cMap = (Map)json;
			if (cMap != null && !cMap.isEmpty()) {
				Iterator it = cMap.keySet().iterator();
				while(it.hasNext()){
					String name = (String)it.next();
					if(!cMap.get(name).equals(null)){
						if(name.equals("isDeal")){//借阅状态 查询
							criteria.andIsDealEqualTo((String)cMap.get(name));
						}else if(name.equals("isOvertime")){//是否超期 查询
							criteria.andIsOvertimeEqualTo((String)cMap.get(name));
						}
						/*else if(name.equals("borrowTime")){//借出时间 排序 
							bex.setOrderByClause("borrow_Time  "+cMap.get(name));
						}*/
					}
				}
			}
		}
		int allRecord = clusterBorrowRecordMapper.countByExample(bex);
		PaginationUtil pagination = new PaginationUtil((begin-1)*length,length,allRecord);
		bex.setPagination(pagination);
		List<BorrowRecord> borrowList = clusterBorrowRecordMapper.selectByExample(bex);
		resultMap.put("allRecord", allRecord);
		resultMap.put("borrowList", borrowList);
		return resultMap;
	}
	
	public int deleteBorrow(String borrowId){
		return masterBorrowRecordMapper.deleteByPrimaryKey(borrowId);
	}
	
	/**
	 * 根据userId查询借阅记录
	 * */
	public List<BorrowRecord> selectBorrowRecordByUserId(String userId){
		BorrowRecordExample bex = new BorrowRecordExample();
		bex.setOrderByClause("is_Deal asc,borrow_Time desc");
		Criteria criteria = bex.createCriteria();
		criteria.andUserIdEqualTo(userId);
		return clusterBorrowRecordMapper.selectByExample(bex);
	}
	
	/**
	 * 根据userId查询目前借阅状态的图书数量
	 * */
	public int selectCurrnetBorrowNumByUserId(String userId){
		BorrowRecordExample bex = new BorrowRecordExample();
		Criteria criteria = bex.createCriteria();
		criteria.andUserIdEqualTo(userId);
		criteria.andIsDealNotEqualTo("2");
		return clusterBorrowRecordMapper.countByExample(bex);
	}
}
